#!/usr/bin/perl

# copy-paste the character table from the wiki as input for this script
# http://code.google.com/p/bangaio-tools/wiki/CharacterSet

use strict;
use Encode qw(encode_utf8);

my @table;

binmode STDIN,":utf8";

while(<>)
{
	while(/([0-9A-F]{2})(?:\s*\|\|\s*|\t)(.)/gi)
	{
		$table[hex($1)]=$2;
	}
}

print "#include <stdlib.h>\n";
print "\n";
print "char *charset_table[256]=\n";
print "{\n";

for(0..255) { print "\t".cstring($table[$_]).",\n" }

print "};\n";

sub cstring($)
{
	my $char=shift;

	return "NULL" if !defined($char) or $char eq "";
	return '" "' if $char eq "\t"; # special case

	my $bytes=encode_utf8($char);
	return '"'.(join "",map {
		my $c=ord $_;
		if($_ eq '"' or $_ eq '\\') { "\\$_" }
		elsif($c>=20&&$c<=127) { $_ }
		else { sprintf("\\%03o",$c) }
	} split //,$bytes).'"';
}

